package cn.edu.nchu.fragmentdemo.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Switch;

import androidx.fragment.app.Fragment;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;

import cn.edu.nchu.fragmentdemo.MainActivity;
import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.bean.Route;
import cn.edu.nchu.fragmentdemo.customcomponents.UavCustomSeekbar;
import cn.edu.nchu.fragmentdemo.db.dao.RouteDao;
import lombok.SneakyThrows;


/**
 * @author Tsuki
 */
public class UavRouteSettingFragment extends Fragment {


    private Route currentRoute;
    /**
     * 飞行高度设置条
     */
    private UavCustomSeekbar flightHeight;

    /**
     * 是否使用海拔高度开关
     */
    private Switch uesAltitude;

    /**
     * 飞行速度设置条
     */
    private UavCustomSeekbar flightSpeed;

    /**
     * 偏航角设置
     */
    private Spinner yawAngle;

    /**
     * 任务完成动作设置
     */
    private Spinner taskComletionAction;

    /**
     * 失控保护动作设置
     */
    private Spinner runawayProtectAction;

    /**
     * 悬停时间
     */
    private UavCustomSeekbar hoverTime;
    /**
     * 上升速度
     */
    private UavCustomSeekbar rise_speed;

    /**
     * 下降速度
     */
    private UavCustomSeekbar drop_speed;

    /**
     * 航线Dao
     */
    Dao<Route, Integer> routeDao;

    private MainActivity mainActivity;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_uav_route_setting, container, false);
        flightHeight = rootView.findViewById(R.id.flight_height);
        uesAltitude = rootView.findViewById(R.id.altitude_switch);
        flightSpeed = rootView.findViewById(R.id.flight_speed);
        yawAngle = rootView.findViewById(R.id.yaw_angle);
        taskComletionAction = rootView.findViewById(R.id.task_comletion_action);
        runawayProtectAction = rootView.findViewById(R.id.runaway_protect_action);
        hoverTime = rootView.findViewById(R.id.hover_time);
        rise_speed = rootView.findViewById(R.id.rise_speed);
        drop_speed = rootView.findViewById(R.id.drop_speed);
        return rootView;
    }


    @SneakyThrows
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        routeDao = RouteDao.getRouteDao(requireContext());
        init();
    }

    private void init() throws SQLException {
        //从bundle中读取传递来的航线id
        Bundle bundle = getArguments();
        if (bundle != null) {
            int value = bundle.getInt("routeId");
            //根据传来的id初始化当前航线设置的航线
            currentRoute = getCurrentRouteByID(value);
        }
        if (currentRoute != null) {
            flightHeight.getSeekBar().setProgress(Double.valueOf(currentRoute.getFlyHeight()).intValue());
            //根据当前航线的是否使用海拔高度（ 0/1  不使用/使用 ）来初始化switch
            uesAltitude.setChecked(currentRoute.getUseAltitude() == 1);
            flightSpeed.getSeekBar().setProgress(Double.valueOf(currentRoute.getFlySpeed()).intValue());

            String[] aircraftYawAngles = getResources().getStringArray(R.array.aircraft_yaw_angle);
            // 创建一个 ArrayAdapter，并将字符串数组设置为其数据源
            ArrayAdapter<String> yawAngleAdapter = new ArrayAdapter<>(requireContext(), R.layout.spinner_item, aircraftYawAngles);  // 使用自定义的spinner_item.xml布局文件
            // 设置下拉列表的样式
            yawAngleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            yawAngle.setAdapter(yawAngleAdapter);
            yawAngle.setSelection(currentRoute.getFlyYawAngle());

            String[] taskComletionActions = getResources().getStringArray(R.array.task_comletion_action);
            // 创建一个 ArrayAdapter，并将字符串数组设置为其数据源
            ArrayAdapter<String> taskComletionActionAdapter = new ArrayAdapter<>(requireContext(),  R.layout.spinner_item, taskComletionActions);
            taskComletionActionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            taskComletionAction.setAdapter(taskComletionActionAdapter);
            taskComletionAction.setSelection(currentRoute.getTaskFinishAction());

            String[] runawayProtectActions = getResources().getStringArray(R.array.runaway_protect_action);
            // 创建一个 ArrayAdapter，并将字符串数组设置为其数据源
            ArrayAdapter<String> runawayProtectActionAdapter = new ArrayAdapter<>(requireContext(),  R.layout.spinner_item, runawayProtectActions);
            runawayProtectActionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            runawayProtectAction.setAdapter(runawayProtectActionAdapter);
            runawayProtectAction.setSelection(currentRoute.getOutOfControlProtectAction());

            hoverTime.getSeekBar().setProgress(Double.valueOf(currentRoute.getHoverTime()).intValue());

            rise_speed.getSeekBar().setProgress(Double.valueOf(currentRoute.getUpSpeed()).intValue() * 10);

            drop_speed.getSeekBar().setProgress(Double.valueOf(currentRoute.getDownSpeed()).intValue() * 10);


        }
    }


    /**
     * 根据航线的id从数据库中查询返回航线的实体
     *
     * @param routeId
     * @return Route
     * @throws SQLException
     */
    private Route getCurrentRouteByID(int routeId) throws SQLException {
        Route route;
        route = routeDao.queryForId(routeId);
        return route;
    }


}
